﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.ApplicationServices;
using System.Windows;

namespace OrganizeYourSearch.Startups
{
    /// <summary>
    /// Wraps the original WPFApplication. Takes care of command line arguments from IE, to open a new document
    /// from the data and metadata received from IE, and prevents multiple instance being created.
    /// </summary>
    public class SingleInstanceAppWrapper : WindowsFormsApplicationBase
    {
        //Create a WPF app Class
        WPFApplication myWpfApp;
        public SingleInstanceAppWrapper()
        {
            this.IsSingleInstance = true;
        }

        /// <summary>
        /// Event to hanlde when first instance of the application is starting. If IE is triggering its opening, 
        /// open the document sent from cmmand line arguments in a new tab, else just display the homepage
        /// </summary>
        /// <param name="eventArgs">Command Line Arguments (Where we send the document from IE and its metadata)</param>
        /// <returns>Should the application continue running or not</returns>
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            myWpfApp = new WPFApplication();
            myWpfApp.Run();

            if (eventArgs.CommandLine.Count > 0)
            {
                try
                {
                    string[] str = eventArgs.CommandLine.ToArray<string>();
                    MainWindow win = myWpfApp.MainWindow as MainWindow;
                    if (win != null)
                    {
                        win.Method_AddNewTab(str);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception from OnStartupNextInstance:\n" + ex.Message);
                }
            }

            return true;
        }

        /// <summary>
        /// When IE triggers another instance of the application when it has already started. Don't allow the
        /// second instance to be created. Instead, let a new tab be opened in the same instance
        /// </summary>
        /// <param name="eventArgs">Command Line Arguments (Where we send the document from IE and its metadata)</param>
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            if (eventArgs.CommandLine.Count > 0)
            {
                try
                {
                    string[] str = eventArgs.CommandLine.ToArray<string>();
                    MainWindow win = myWpfApp.MainWindow as MainWindow;
                    if (win != null)
                    {
                        win.Method_AddNewTab(str);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception from OnStartupNextInstance:\n" + ex.Message);
                }
            }
        }

    }
}
